# ZLM媒体服务器设置脚本
Write-Host "=== ZLM媒体服务器设置向导 ===" -ForegroundColor Green

# 检查是否有Docker
Write-Host "`n检查Docker是否可用..." -ForegroundColor Yellow
try {
    $dockerVersion = docker --version 2>$null
    if ($dockerVersion) {
        Write-Host "✓ 发现Docker: $dockerVersion" -ForegroundColor Green
        $hasDocker = $true
    } else {
        $hasDocker = $false
    }
} catch {
    $hasDocker = $false
}

if (-not $hasDocker) {
    Write-Host "❌ 未发现Docker" -ForegroundColor Red
}

# 检查端口占用
Write-Host "`n检查端口占用情况..." -ForegroundColor Yellow
$ports = @(80, 8080, 1935, 8554, 10000)
$availablePorts = @()
$occupiedPorts = @()

foreach ($port in $ports) {
    $portCheck = netstat -an | findstr ":$port "
    if ($portCheck) {
        $occupiedPorts += $port
        Write-Host "  端口 $port : ❌ 已占用" -ForegroundColor Red
    } else {
        $availablePorts += $port
        Write-Host "  端口 $port : ✓ 可用" -ForegroundColor Green
    }
}

# 提供解决方案
Write-Host "`n=== 解决方案选择 ===" -ForegroundColor Cyan

if ($hasDocker -and $availablePorts.Count -ge 3) {
    Write-Host "`n方案1: 使用Docker运行ZLM (推荐)" -ForegroundColor Green
    Write-Host "优点: 简单快速，自动配置" -ForegroundColor White
    Write-Host "执行命令:" -ForegroundColor Yellow
    
    if ($availablePorts -contains 8080) {
        Write-Host "docker run -d -p 1935:1935 -p 8080:80 -p 8443:443 -p 8554:554 -p 10000:10000 -p 10000:10000/udp --name zlm zlmediakit/zlmediakit:master" -ForegroundColor White
        Write-Host "ZLM将在端口8080运行 (已在WVP配置中设置)" -ForegroundColor Green
    } elseif ($availablePorts -contains 80) {
        Write-Host "docker run -d -p 1935:1935 -p 80:80 -p 8443:443 -p 8554:554 -p 10000:10000 -p 10000:10000/udp --name zlm zlmediakit/zlmediakit:master" -ForegroundColor White
        Write-Host "ZLM将在端口80运行" -ForegroundColor Green
    } else {
        $altPort = $availablePorts[0]
        Write-Host "docker run -d -p 1935:1935 -p ${altPort}:80 -p 8443:443 -p 8554:554 -p 10000:10000 -p 10000:10000/udp --name zlm zlmediakit/zlmediakit:master" -ForegroundColor White
        Write-Host "ZLM将在端口$altPort运行 (需要修改WVP配置)" -ForegroundColor Yellow
    }
}

Write-Host "`n方案2: 手动下载ZLM" -ForegroundColor Green
Write-Host "1. 访问: https://github.com/ZLMediaKit/ZLMediaKit/releases" -ForegroundColor White
Write-Host "2. 下载Windows版本的最新release" -ForegroundColor White
Write-Host "3. 解压并运行MediaServer.exe" -ForegroundColor White

Write-Host "`n方案3: 临时禁用ZLM检查" -ForegroundColor Yellow
Write-Host "如果暂时不需要视频功能，可以修改配置禁用ZLM检查" -ForegroundColor White

Write-Host "`n=== 当前WVP配置 ===" -ForegroundColor Cyan
Write-Host "ZLM服务器地址: 127.0.0.1:8080" -ForegroundColor White
Write-Host "如果ZLM运行在其他端口，需要修改application.yml中的http-port配置" -ForegroundColor Yellow

Write-Host "`n=== 快速启动命令 ===" -ForegroundColor Green

if ($hasDocker) {
    Write-Host "`n使用Docker启动ZLM:" -ForegroundColor Yellow
    if ($availablePorts -contains 8080) {
        Write-Host "docker run -d -p 8080:80 -p 1935:1935 -p 8554:554 -p 10000:10000/udp --name zlm zlmediakit/zlmediakit:master" -ForegroundColor White
        Write-Host "`n启动后，ZLM将在 http://127.0.0.1:8080 运行" -ForegroundColor Green
        Write-Host "WVP配置已设置为连接此地址" -ForegroundColor Green
    } else {
        Write-Host "需要先释放端口8080或修改配置使用其他端口" -ForegroundColor Red
    }
    
    Write-Host "`n检查ZLM状态:" -ForegroundColor Yellow
    Write-Host "docker ps | findstr zlm" -ForegroundColor White
    
    Write-Host "`n查看ZLM日志:" -ForegroundColor Yellow
    Write-Host "docker logs zlm" -ForegroundColor White
    
    Write-Host "`n停止ZLM:" -ForegroundColor Yellow
    Write-Host "docker stop zlm && docker rm zlm" -ForegroundColor White
}

Write-Host "`n=== 验证ZLM连接 ===" -ForegroundColor Green
Write-Host "ZLM启动后，可以通过以下方式验证:" -ForegroundColor White
Write-Host "1. 浏览器访问: http://127.0.0.1:8080" -ForegroundColor Gray
Write-Host "2. 检查WVP日志中是否还有连接失败的错误" -ForegroundColor Gray
Write-Host "3. 在WVP管理界面查看媒体服务器状态" -ForegroundColor Gray

Write-Host "`n=== 下一步操作 ===" -ForegroundColor Green
Write-Host "1. 选择并执行上述方案之一启动ZLM" -ForegroundColor White
Write-Host "2. 重新启动WVP应用程序" -ForegroundColor White
Write-Host "3. 检查连接状态" -ForegroundColor White

$choice = Read-Host "`n是否现在使用Docker启动ZLM? (y/n)"
if ($choice -eq 'y' -or $choice -eq 'Y') {
    if ($hasDocker -and ($availablePorts -contains 8080)) {
        Write-Host "`n正在启动ZLM..." -ForegroundColor Yellow
        try {
            docker run -d -p 8080:80 -p 1935:1935 -p 8554:554 -p 10000:10000/udp --name zlm zlmediakit/zlmediakit:master
            Write-Host "✓ ZLM启动成功!" -ForegroundColor Green
            Write-Host "ZLM现在运行在 http://127.0.0.1:8080" -ForegroundColor Green
            Write-Host "请重新启动WVP应用程序以连接ZLM" -ForegroundColor Yellow
        } catch {
            Write-Host "❌ ZLM启动失败: $($_.Exception.Message)" -ForegroundColor Red
        }
    } else {
        Write-Host "❌ 无法自动启动ZLM，请手动执行上述命令" -ForegroundColor Red
    }
}

Read-Host "`n按回车键退出"
